package com.lkd.job;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.lkd.common.VMSystem;
import com.lkd.entity.VendingMachineEntity;
import com.lkd.feign.VMService;
import com.lkd.service.VendingMachineService;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.xxl.job.core.util.ShardingUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 扫描售货机
 */
@Component
public class GetVmChannelJob {
    @Autowired
    private VendingMachineService vmService;

    @XxlJob("supplyJobHarder")
    public ReturnT<String> supplyJobHarder(String param){
        ShardingUtil.ShardingVO shardingVo = ShardingUtil.getShardingVo();
        int total = shardingVo.getTotal();//分片总数
        int index = shardingVo.getIndex();//当前分片索引

        //查询所有运行中的售货机
        List<VendingMachineEntity> list = vmService.list(Wrappers.<VendingMachineEntity>lambdaQuery()
                .eq(VendingMachineEntity::getVmStatus, VMSystem.VM_STATUS_RUNNING)
                .apply("mod (id , "+ total +") = " + index));

        for (VendingMachineEntity vendingMachineEntity : list) {
            vmService.sendChannelToCreatTask(vendingMachineEntity.getInnerCode());
        }
        return ReturnT.SUCCESS;
    }

}
